# ============================================================================
# 构建类型配置模块
# ============================================================================
# 提供不同构建类型（Release/Debug/DynamicRelease/DynamicDebug）的配置
# ============================================================================

# 检测是否使用分布式编译
set(USING_DISTRIBUTED_COMPILE OFF)
if(DEFINED ENV{DISTCC_HOSTS} OR DEFINED ENV{ICECC_SCHEDULER_HOST} OR DEFINED ENV{YADCC_COORDINATOR_ADDR})
    set(USING_DISTRIBUTED_COMPILE ON)
    message(STATUS "检测到分布式编译环境，将调整优化策略")
    if(DEFINED ENV{DISTCC_HOSTS})
        message(STATUS "  模式: distcc")
    elseif(DEFINED ENV{ICECC_SCHEDULER_HOST})
        message(STATUS "  模式: icecc")
    elseif(DEFINED ENV{YADCC_COORDINATOR_ADDR})
        message(STATUS "  模式: yadcc")
    endif()
endif()

# 检测 CentOS 8，需要禁用 C++11 ABI 以匹配预编译的 protobuf 库
# 注意：DISTRO_ID 和 DISTRO_VERSION_ID 应该在 PlatformConfig.cmake 中已设置
set(USE_OLD_CXX_ABI OFF)
if(UNIX)
    # 如果 PlatformConfig 未设置，尝试检测
    if(NOT DEFINED DISTRO_ID)
        if(EXISTS "/etc/os-release")
            file(READ "/etc/os-release" OS_RELEASE_CONTENT)
            string(REGEX MATCH "ID=\"?([^\"\n]*)\"?" ID_MATCH "${OS_RELEASE_CONTENT}")
            if(ID_MATCH)
                string(REGEX REPLACE "ID=\"?([^\"]*)\"?" "\\1" DISTRO_ID "${ID_MATCH}")
                string(TOLOWER "${DISTRO_ID}" DISTRO_ID)
            endif()
            string(REGEX MATCH "VERSION_ID=\"?([^\"\n]*)\"?" VERSION_MATCH "${OS_RELEASE_CONTENT}")
            if(VERSION_MATCH)
                string(REGEX REPLACE "VERSION_ID=\"?([^\"]*)\"?" "\\1" DISTRO_VERSION_ID "${VERSION_MATCH}")
            endif()
        endif()
    endif()
    
    # 检测 CentOS 8
    if(DISTRO_ID STREQUAL "centos" AND DISTRO_VERSION_ID STREQUAL "8")
        set(USE_OLD_CXX_ABI ON)
        message(STATUS "检测到 CentOS 8，将禁用 C++11 ABI 以匹配预编译的 protobuf 库")
    endif()
endif()

# ============================================================================
# Release 构建类型配置
# ============================================================================
if(CMAKE_BUILD_TYPE STREQUAL "Release")
    set(CMAKE_BUILD_TYPE "Release")
    
    if(UNIX)
        # Linux Release 配置
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -fPIC")
        
        # 静态链接 C++ 标准库
        if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
            # 启用链接时优化（LTO）
            if(CMAKE_BUILD_LTO AND NOT USING_DISTRIBUTED_COMPILE)
                set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
                set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
                set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
                set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto")
                message(STATUS "✅ 已启用链接时优化 (LTO)")
            elseif(CMAKE_BUILD_LTO AND USING_DISTRIBUTED_COMPILE)
                message(STATUS "⚠️  LTO 已请求但已禁用（与分布式编译不兼容）")
            endif()
        elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
            if(CMAKE_BUILD_LTO AND NOT USING_DISTRIBUTED_COMPILE)
                set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=thin")
                set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto=thin")
                set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto=thin")
                set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto=thin")
                message(STATUS "✅ 已启用链接时优化 (LTO thin)")
            elseif(CMAKE_BUILD_LTO AND USING_DISTRIBUTED_COMPILE)
                message(STATUS "⚠️  LTO 已请求但已禁用（与分布式编译不兼容）")
            endif()
        endif()
        
        add_definitions(
            -DNDEBUG
            -Wcomment
            -DNF_STATIC_PLUGIN
            -DEVPP_HTTP_CLIENT_SUPPORTS_SSL
            -DEVPP_HTTP_SERVER_SUPPORTS_SSL
            -DH_HAVE_CAMERON314_CONCURRENTQUEUE
		    -DCOMPILE_CHECK_STRING_FORMAT
            -DLUA_COMPAT_5_1
            -DLUA_USE_LINUX
            -fPIC
            -Wno-error
            -Wpointer-arith
        )
        
        # CentOS 8 需要禁用 C++11 ABI 以匹配预编译的 protobuf 库
        if(USE_OLD_CXX_ABI)
            add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
            message(STATUS "✅ 已禁用 C++11 ABI（使用旧 ABI）")
        endif()
        
        # 设置库路径（使用构建目录下的 lib）
        if(DISTRO_ID STREQUAL "centos" OR DISTRO_ID STREQUAL "rhel")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release"
                "${CMAKE_BINARY_DIR}/lib")
        elseif(DISTRO_ID STREQUAL "ubuntu" OR DISTRO_ID STREQUAL "debian")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release/ubuntu"
                "${CMAKE_BINARY_DIR}/lib")
        else()
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release"
                "${CMAKE_BINARY_DIR}/lib")
        endif()
        
        set(LIB_LINK_DEP resolv dl rt pthread libtirpc libprotobuf.a libCommon.a libLuaBind.a libNFMessageDefine.a libNFCore.a libNFPluginModule.a)
        set(LIB_LINK_NF libNFKernelPlugin.a NFTestPlugin.a)
        
    else(WIN32)
        # Windows Release 配置
        set(CMAKE_CXX_FLAGS_RELEASE "/MT /bigobj")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Z7 /O2 -DNDEBUG -DCOMPILE_CHECK_STRING_FORMAT -DWIN32_LEAN_AND_MEAN -D_WINSOCK_DEPRECATED_NO_WARNINGS -D_LIB -D_WIN32 -D_CONSOLE -DNF_NONCLIENT_BUILD -DUNICODE -D_UNICODE -D_WINDOWS -DWIN32 -DNF_STATIC_PLUGIN -DLUA_COMPAT_5_1 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USRDLL -DDLL_EXPORTS -DMYSQLPP_MAKING_DLL -DHAVE_MYSQL_SSL_SET -DBUILDING_LIBCURL -DCURL_STATICLIB -D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -DNOMINMAX")
        
        # MSVC 链接时优化
        if(CMAKE_BUILD_LTO AND MSVC AND NOT USING_DISTRIBUTED_COMPILE)
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /GL")
            set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /GL")
            set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /LTCG")
            set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /LTCG")
            message(STATUS "✅ 已启用链接时优化 (LTCG)")
        elseif(CMAKE_BUILD_LTO AND MSVC AND USING_DISTRIBUTED_COMPILE)
            message(STATUS "⚠️  LTO 已请求但已禁用（与分布式编译不兼容）")
        endif()
        
        set(LIB_DIR 
            ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
            ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release"
            "${CMAKE_NFSHM_SOURCE_DIR}/Build/Lib/Release"
            "${CMAKE_NFSHM_SOURCE_DIR}/Build/Lib/Release/Release")
    endif()

# ============================================================================
# Debug 构建类型配置
# ============================================================================
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(CMAKE_BUILD_TYPE "Debug")
    
    if(UNIX)
        # Linux Debug 配置
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -Wall -g3 -ggdb -fPIC")
        
        # 静态链接 C++ 标准库
        if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
        elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
            # Clang 配置
        endif()
        
        add_definitions(
            -D_DEBUG
            -DCHECK_MEM_LEAK
            -Wcomment
            -DNF_STATIC_PLUGIN
            -DEVPP_HTTP_CLIENT_SUPPORTS_SSL
            -DEVPP_HTTP_SERVER_SUPPORTS_SSL
            -DH_HAVE_CAMERON314_CONCURRENTQUEUE
		    -DCOMPILE_CHECK_STRING_FORMAT
            -DLUA_COMPAT_5_1
            -DLUA_USE_LINUX
            -fPIC
            -Wno-error
            -Wpointer-arith
        )
        
        # CentOS 8 需要禁用 C++11 ABI 以匹配预编译的 protobuf 库
        if(USE_OLD_CXX_ABI)
            add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
            message(STATUS "✅ 已禁用 C++11 ABI（使用旧 ABI）")
        endif()
        
        # 设置库路径（使用构建目录下的 lib）
        if(DISTRO_ID STREQUAL "centos" OR DISTRO_ID STREQUAL "rhel")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug"
                "${CMAKE_BINARY_DIR}/lib")
        elseif(DISTRO_ID STREQUAL "ubuntu" OR DISTRO_ID STREQUAL "debian")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug/ubuntu"
                "${CMAKE_BINARY_DIR}/lib")
        else()
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug"
                "${CMAKE_BINARY_DIR}/lib")
        endif()
        
        set(LIB_LINK_DEP resolv dl rt pthread libtirpc libprotobuf.a libCommon.a libLuaBind.a libNFMessageDefine.a libNFCore.a libNFPluginModule.a)
        set(LIB_LINK_NF libNFKernelPlugin.a NFTestPlugin.a)
        
    else(WIN32)
        # Windows Debug 配置
        if(MSVC)
            set(CMAKE_CXX_FLAGS_DEBUG "/Z7 /Ob0 /Od /RTC1 /DEBUG /MTd /bigobj /FS")
            set(CMAKE_C_FLAGS_DEBUG "/Z7 /Ob0 /Od /RTC1 /DEBUG /MTd /FS")
            
            # 强制使用 /MTd
            string(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
            string(REPLACE "/MD" "/MT" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
        endif()
        
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOMPILE_CHECK_STRING_FORMAT -DWIN32_LEAN_AND_MEAN -D_WINSOCK_DEPRECATED_NO_WARNINGS -D_LIB -D_WIN32 -D_CONSOLE -DNF_NONCLIENT_BUILD -DUNICODE -D_UNICODE -D_WINDOWS -DWIN32 -D_DEBUG -DNF_STATIC_PLUGIN -DLUA_COMPAT_5_1 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USRDLL -DDLL_EXPORTS -DMYSQLPP_MAKING_DLL -DHAVE_MYSQL_SSL_SET -DBUILDING_LIBCURL -DCURL_STATICLIB -D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS -DNOMINMAX")
        
        set(LIB_DIR 
            ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
            ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
            "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug"
            "${CMAKE_NFSHM_SOURCE_DIR}/Build/Lib/Debug"
            "${CMAKE_NFSHM_SOURCE_DIR}/Build/Lib/Debug/Debug")
    endif()

# ============================================================================
# DynamicDebug 构建类型配置
# ============================================================================
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicDebug")
    set(CMAKE_BUILD_TYPE "DynamicDebug")
    
    if(UNIX)
        # Linux DynamicDebug 配置
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -Wall -g3 -ggdb -fPIC")
        
        # 静态链接 C++ 标准库
        if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
        elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
            # Clang 配置
        endif()
        
        add_definitions(
            -D_DEBUG
            -DCHECK_MEM_LEAK
            -Wcomment
            -DLUA_COMPAT_5_1
            -DLUA_USE_LINUX
            -DNF_DYNAMIC_PLUGIN
            -DEVPP_HTTP_CLIENT_SUPPORTS_SSL
            -DEVPP_HTTP_SERVER_SUPPORTS_SSL
            -DH_HAVE_CAMERON314_CONCURRENTQUEUE
		    -DCOMPILE_CHECK_STRING_FORMAT
            -fPIC
            -Wno-error
            -Wpointer-arith
        )
        
        # CentOS 8 需要禁用 C++11 ABI 以匹配预编译的 protobuf 库
        if(USE_OLD_CXX_ABI)
            add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
            message(STATUS "✅ 已禁用 C++11 ABI（使用旧 ABI）")
        endif()
        
        # 设置库路径（使用构建目录下的 lib）
        if(DISTRO_ID STREQUAL "centos" OR DISTRO_ID STREQUAL "rhel")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug"
                "${CMAKE_BINARY_DIR}/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Dynamic_Debug")
        elseif(DISTRO_ID STREQUAL "ubuntu" OR DISTRO_ID STREQUAL "debian")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug/ubuntu"
                "${CMAKE_BINARY_DIR}/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Dynamic_Debug")
        else()
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_debug"
                "${CMAKE_BINARY_DIR}/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Dynamic_Debug")
        endif()
        
        set(LIB_LINK_DEP resolv dl rt pthread libtirpc)
        
    else(WIN32)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOMPILE_CHECK_STRING_FORMAT -DWIN32_LEAN_AND_MEAN -D_WINSOCK_DEPRECATED_NO_WARNINGS -D_LIB -D_WIN32 -D_CONSOLE -DNF_NONCLIENT_BUILD -DUNICODE -D_UNICODE -D_WINDOWS -DWIN32 -D_DEBUG -DNF_STATIC_PLUGIN -DLUA_COMPAT_5_1 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USRDLL -DDLL_EXPORTS -DMYSQLPP_MAKING_DLL -DHAVE_MYSQL_SSL_SET -DBUILDING_LIBCURL -DCURL_STATICLIB")
    endif()

# ============================================================================
# DynamicRelease 构建类型配置
# ============================================================================
elseif(CMAKE_BUILD_TYPE STREQUAL "DynamicRelease")
    set(CMAKE_BUILD_TYPE "DynamicRelease")
    
    if(UNIX)
        # Linux DynamicRelease 配置
        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -fPIC")
        
        # 静态链接 C++ 标准库
        if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
            set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static-libstdc++")
        elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
            # Clang 配置
        endif()
        
        add_definitions(
            -DNDEBUG
            -Wcomment
            -DLUA_COMPAT_5_1
            -DLUA_USE_LINUX
            -DNF_DYNAMIC_PLUGIN
            -DEVPP_HTTP_CLIENT_SUPPORTS_SSL
            -DEVPP_HTTP_SERVER_SUPPORTS_SSL
            -DH_HAVE_CAMERON314_CONCURRENTQUEUE
		    -DCOMPILE_CHECK_STRING_FORMAT
            -fPIC
            -Wno-error
            -Wpointer-arith
        )
        
        # CentOS 8 需要禁用 C++11 ABI 以匹配预编译的 protobuf 库
        if(USE_OLD_CXX_ABI)
            add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
            message(STATUS "✅ 已禁用 C++11 ABI（使用旧 ABI）")
        endif()
        
        # 设置库路径（使用构建目录下的 lib）
        if(DISTRO_ID STREQUAL "centos" OR DISTRO_ID STREQUAL "rhel")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release"
                "${CMAKE_BINARY_DIR}/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Dynamic_Release")
        elseif(DISTRO_ID STREQUAL "ubuntu" OR DISTRO_ID STREQUAL "debian")
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release/ubuntu"
                "${CMAKE_BINARY_DIR}/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Dynamic_Release")
        else()
            set(LIB_DIR "/usr/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/thirdparty/lib64_release"
                "${CMAKE_BINARY_DIR}/lib"
                "${CMAKE_NFSHM_SOURCE_DIR}/Install/Bin/Dynamic_Release")
        endif()
        
        set(LIB_LINK_DEP resolv dl rt pthread libtirpc)
        
    else(WIN32)
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCOMPILE_CHECK_STRING_FORMAT -DWIN32_LEAN_AND_MEAN -D_WINSOCK_DEPRECATED_NO_WARNINGS -D_LIB -D_WIN32 -D_CONSOLE -DNF_NONCLIENT_BUILD -DUNICODE -D_UNICODE -D_WINDOWS -DWIN32 -DNDEBUG -DNF_DYNAMIC_PLUGIN -DLUA_COMPAT_5_1 -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_USRDLL -DDLL_EXPORTS -DMYSQLPP_MAKING_DLL -DHAVE_MYSQL_SSL_SET -DBUILDING_LIBCURL -DCURL_STATICLIB")
    endif()
endif()

# 应用库目录
if(DEFINED LIB_DIR)
    link_directories(${LIB_DIR})
endif()

# 输出构建类型信息
message(STATUS "构建类型: ${CMAKE_BUILD_TYPE}")
if(DEFINED LIB_DIR)
    message(STATUS "库目录: ${LIB_DIR}")
endif()

